package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseApp;
import lib.quasar.util.ToastUtil;

/**
 * description: 化验单, 增加
 * created by kalu on 2018/4/16 13:15
 */
public class AssayTitleDialog extends BaseDialog implements View.OnClickListener {

    private String menuStr, nameStr, resultStr, maxStr, minStr, unitStr, titleStr;
    /************************************************/

    private OnDialogChangeListener listener;

    public AssayTitleDialog(Activity activity) {
        super(activity);

    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void cancel() {

        if (null != listener &&
                (!TextUtils.isEmpty(menuStr) ||
                        !TextUtils.isEmpty(nameStr) ||
                        !TextUtils.isEmpty(resultStr) ||
                        !TextUtils.isEmpty(maxStr) ||
                        !TextUtils.isEmpty(minStr) ||
                        !TextUtils.isEmpty(unitStr))) {
            listener.onDialogChange(menuStr, nameStr, resultStr, maxStr, minStr, unitStr);
        }

        super.cancel();
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_assay_title;
    }

    @Override
    protected void initData() {

        findViewById(R.id.layout_dialog_assay_title_back).setOnClickListener(this);
        findViewById(R.id.layout_dialog_assay_title_ok).setOnClickListener(this);

        final EditText menu = findViewById(R.id.dialog_assay_title_menu);
        if (null != menu) {
            menuStr = menu.getText().toString().trim();
            menu.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    menuStr = editable.toString();
                }
            });
        }

        final EditText name = findViewById(R.id.dialog_assay_title_name);
        if (null != name) {
            nameStr = name.getText().toString().trim();
            name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    nameStr = editable.toString();
                }
            });
        }

        final EditText result = findViewById(R.id.dialog_assay_title_result);
        if (null != result) {
            resultStr = result.getText().toString().trim();
            result.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    resultStr = editable.toString();
                }
            });
        }

        final EditText max = findViewById(R.id.dialog_assay_title_max);
        if (null != max) {
            maxStr = max.getText().toString().trim();
            max.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    maxStr = editable.toString();
                }
            });
        }

        final EditText min = findViewById(R.id.dialog_assay_title_min);
        if (null != min) {
            minStr = min.getText().toString().trim();
            min.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    minStr = editable.toString();
                }
            });
        }

        final EditText unit = findViewById(R.id.dialog_assay_title_unit);
        if (null != unit) {
            unitStr = unit.getText().toString().trim();
            unit.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    unitStr = editable.toString();
                }
            });
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.layout_dialog_assay_title_back:
                dismiss();
                break;
            case R.id.layout_dialog_assay_title_ok:
                if (null != listener &&
                        (!TextUtils.isEmpty(menuStr) ||
                                !TextUtils.isEmpty(nameStr) ||
                                !TextUtils.isEmpty(resultStr) ||
                                !TextUtils.isEmpty(maxStr) ||
                                !TextUtils.isEmpty(minStr) ||
                                !TextUtils.isEmpty(unitStr))) {
                    listener.onDialogChange(menuStr, nameStr, resultStr, maxStr, minStr, unitStr);
                }
                if (TextUtils.isEmpty(menuStr)) {
                    ToastUtil.showToast("化验单名称不能为空");
                } else if (TextUtils.isEmpty(nameStr)) {
                    ToastUtil.showToast("化验项名称不能为空");
                } else {
                    dismiss();
                }
                break;
        }
    }

    public void setMenu(String menu) {
        menuStr = minStr;
        final EditText edit = findViewById(R.id.dialog_assay_title_menu);
        if (null != edit && !TextUtils.isEmpty(menu)) {
            edit.setTextColor(getContext().getResources().getColor(R.color.color_bg_theme));
            edit.setText(menu);
            edit.setEnabled(false);
        }
    }

    public void setMenuTitle(String title) {
        titleStr = title;
        final TextView tv = findViewById(R.id.add_child_item);
        if (null != tv && !TextUtils.isEmpty(title)) {
            tv.setTextColor(getContext().getResources().getColor(R.color.color_white));
            tv.setText(titleStr);
            tv.setEnabled(false);
        }
    }

    public void setName(boolean enable) {
        final View view = findViewById(R.id.dialog_assay_line);
        if (null == view) return;
        view.setBackgroundColor(enable ? Color.TRANSPARENT : BaseApp.getResource().getColor(R.color.color_bg_theme));
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String menu, String name, String result, String max, String min, String unit);
    }
}
